﻿using ED.Common;
using ED.Common.BaseModel;
using ED.Common.Extensions;
using ED.IRepository;
using ED.IService;
using ED.IService.User;
using ED.Model;

namespace ED.Service.User
{
    public class UserService : IUserService
    {
        private readonly IUserRepository userRepository;
        private readonly IUserRoleRepository userRoleRepository;
        private readonly IUserRoleService userRoleService;
        private readonly IRoleRepository roleRepository;
        public UserService(IUserRepository userRepository, IUserRoleRepository userRoleRepository, IUserRoleService userRoleService, IRoleRepository roleRepository)
        {
            this.userRepository = userRepository;
            this.userRoleRepository = userRoleRepository;
            this.userRoleService = userRoleService;
            this.roleRepository = roleRepository;
        }

        public UserInfoRep Login(UserInfoReq req)
        {
            UserInfoRep res = new UserInfoRep();
            var userEntityList = userRepository.GetList(t => t.UserName == req.UserName);
            if (userEntityList.IsNotNullOrEmpty())
            {
               var userEntity = userEntityList.Where(t => t.Password == req.Password).FirstOrDefault();
                if (userEntity.IsDefault()) 
                {
                    ThrowHelp.Throw("该用户密码错误！");
                }
                if (userEntity.Active == false)
                {
                    ThrowHelp.Throw("该用户已被禁止登录！");
                }
                var userRoleList = userRoleRepository.GetList(t => t.UserId == userEntity.Id)?.Select(t => t.RoleId);
                var roleList = roleRepository.GetList(t => userRoleList.Contains(t.Id)).Select(t => t.Code);
                res.UserId = userEntity.Id;
                res.UserRole = string.Join(",", roleList);
            }else
            {
                ThrowHelp.Throw("该用户不存在！");
            }

            return res;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Add(int req)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Delete(int req)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public PageOut<int> Page(int req)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Update(int req)
        {
            throw new NotImplementedException();
        }
    }
}
